﻿using Microsoft.AspNetCore.Identity;
using System.Data;

namespace TrackSystem.API.Extensions
{
    /// <summary>
    /// 错误消息中文化
    /// </summary>
    public class CustomIdentityErrorDescriber : IdentityErrorDescriber
    {
        /// <summary>
        ///  Returns an Microsoft.AspNetCore.Identity.IdentityError indicating a concurrenc failure.
        /// </summary>
        /// <returns></returns>
        public override IdentityError ConcurrencyFailure()
        {
            return new IdentityError
            {
                Code = nameof(ConcurrencyFailure),
                Description = $"内部错误"
            };
        }

        /// <summary>
        /// Returns the default Microsoft.AspNetCore.Identity.IdentityError.
        /// </summary>
        /// <returns></returns>
        public override IdentityError DefaultError()
        {
            return new IdentityError
            {
                Code = nameof(DefaultError),
                Description = $"内部错误"
            };
        }
        
        /// <summary>
        /// Returns an Microsoft.AspNetCore.Identity.IdentityError indicating the specified
        /// email is already associated with an account
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public override IdentityError DuplicateEmail(string email)
        {
            return new IdentityError
            {
                Code = nameof(DuplicateEmail),
                Description = $"电子邮箱{email}已经存在"
            };
        }
        
        /// <summary>
        /// Returns an Microsoft.AspNetCore.Identity.IdentityError indicating the specified
        ///  role name already exists.
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public override IdentityError DuplicateRoleName(string role)
        {
            return new IdentityError
            {
                Code = nameof(DuplicateRoleName),
                Description = $"角色名称{role}已经存在"
            };
        }
        
        /// <summary>
        /// Returns an Microsoft.AspNetCore.Identity.IdentityError indicating the specified
        /// userName already exists.
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public override IdentityError DuplicateUserName(string userName)
        {
            return new IdentityError
            {
                Code = nameof(DuplicateUserName),
                Description = $"电子邮箱{userName}已经存在"
            };
        }
        
        /// <summary>
        ///  Returns an Microsoft.AspNetCore.Identity.IdentityError indicating the specified
        ///  email is invalid.
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public override IdentityError InvalidEmail(string? email)
        {
            return new IdentityError
            {
                Code = nameof(InvalidEmail),
                Description = $"电子邮箱{email}格式错误"
            };
        }
        
        /// <summary>
        /// Returns an Microsoft.AspNetCore.Identity.IdentityError indicating the specified
        /// role name is invalid.
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public override IdentityError InvalidRoleName(string? role)
        {
            return new IdentityError
            {
                Code = nameof(InvalidRoleName),
                Description = $"角色名称{role}不正确"
            };
        }
        
        /// <summary>
        /// Returns an Microsoft.AspNetCore.Identity.IdentityError indicating an invalid token
        /// </summary>
        /// <returns></returns>
        public override IdentityError InvalidToken()
        {
            return new IdentityError
            {
                Code = nameof(InvalidToken),
                Description = $"Token不正确"
            };
        }
 
        /// <summary>
        /// Returns an Microsoft.AspNetCore.Identity.IdentityError indicating the specified
        /// user userName is invalid.
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public override IdentityError InvalidUserName(string? userName)
        {
            return new IdentityError
            {
                Code = nameof(InvalidUserName),
                Description = $"非法用户名{userName}"
            };
        }

        /// <summary>
        ///  Returns an Microsoft.AspNetCore.Identity.IdentityError indicating an external
        ///  login is already associated with an account.
        /// </summary>
        /// <returns></returns>
        public override IdentityError LoginAlreadyAssociated()
        {
            return new IdentityError
            {
                Code = nameof(LoginAlreadyAssociated),
                Description = $"LoginAlreadyAssociated"
            };
        }
   
        /// <summary>
        /// Returns an Microsoft.AspNetCore.Identity.IdentityError indicating a password mismatch
        /// </summary>
        /// <returns></returns>
        public override IdentityError PasswordMismatch()
        {
            return new IdentityError
            {
                Code = nameof(PasswordMismatch),
                Description = $"密码不正确"
            };
        }
        
        /// <summary>
        ///  Returns an Microsoft.AspNetCore.Identity.IdentityError indicating a password
        /// entered does not contain a numeric character, which is required by the password
        ///  policy.
        /// </summary>
        /// <returns></returns>
        public override IdentityError PasswordRequiresDigit()
        {
            return new IdentityError
            {
                Code = nameof(PasswordRequiresDigit),
                Description = $"密码需要包含数字"
            };
        }
  
        /// <summary>
        /// 密码需要小写字母
        /// </summary>
        /// <returns></returns>
        public override IdentityError PasswordRequiresLower()
        {
            return new IdentityError
            {
                Code = nameof(PasswordRequiresLower),
                Description = $"密码需要包含小写字母"
            };
        }
        
        /// <summary>
        /// 密码需要字母
        /// </summary>
        /// <returns></returns>
        public override IdentityError PasswordRequiresNonAlphanumeric()
        {
            return new IdentityError
            {
                Code = nameof(PasswordRequiresNonAlphanumeric),
                Description = $"密码需要包含数字"
            };
        }
        
        /// <summary>
        /// 密码需要特殊字符
        /// </summary>
        /// <param name="uniqueChars"></param>
        /// <returns></returns>
        public override IdentityError PasswordRequiresUniqueChars(int uniqueChars)
        {
            return new IdentityError
            {
                Code = nameof(PasswordRequiresUniqueChars),
                Description = $"密码需要特殊字符"
            };
        }

        /// <summary>
        /// 密码需要大写字母
        /// </summary>
        /// <returns></returns>
        public override IdentityError PasswordRequiresUpper()
        {
            return new IdentityError
            {
                Code = nameof(PasswordRequiresUpper),
                Description = $"密码需要包含大写字符"
            };
        }
        
        /// <summary>
        /// 密码至长度错误
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public override IdentityError PasswordTooShort(int length)
        {
            return new IdentityError
            {
                Code = nameof(PasswordTooShort),
                Description = $"密码至少需要{length}位"
            };
        }
        
        /// <summary>
        /// Returns an Microsoft.AspNetCore.Identity.IdentityError indicating a recovery
        ///  code was not redeemed.
        /// </summary>
        /// <returns></returns>
        public override IdentityError RecoveryCodeRedemptionFailed()
        {
            return new IdentityError
            {
                Code = nameof(RecoveryCodeRedemptionFailed),
                Description = $"RecoveryCodeRedemptionFailed"
            };
        }
        
        /// <summary>
        /// 用户已经设定了密码
        /// </summary>
        /// <returns></returns>
        public override IdentityError UserAlreadyHasPassword()
        {
            return new IdentityError
            {
                Code = nameof(UserAlreadyHasPassword),
                Description = $"用户已经设定了密码"
            };
        }

        /// <summary>
        /// 用户的这个角色{role}已经存在
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public override IdentityError UserAlreadyInRole(string role)
        {
            return new IdentityError
            {
                Code = nameof(UserAlreadyInRole),
                Description = $"用户的这个角色{role}已经存在"
            };
        }
        
        /// <summary>
        /// 用户锁定且未使能
        /// </summary>
        /// <returns></returns>
        public override IdentityError UserLockoutNotEnabled()
        {
            return new IdentityError
            {
                Code = nameof(UserLockoutNotEnabled),
                Description = $"用户锁定且未使能"
            };
        }
        
        /// <summary>
        /// Returns an Microsoft.AspNetCore.Identity.IdentityError indicating a user is not
        /// in the specified role.
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public override IdentityError UserNotInRole(string role)
        {
            return new IdentityError
            {
                Code = nameof(UserNotInRole),
                Description = $"用户没有角色{role}"
            };
        }
    }
}
